from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from ..models import DayNote
from ..serializers import DayNoteSerializer
import calendar
import datetime


@api_view(['get'])
def month(request):
    today = datetime.date.today()
    monthRange = calendar.monthrange(today.year, today.month)
    queryset = DayNote.objects.filter(user=request.user, create_at__gte=datetime.date(
        today.year, today.month, 1), create_at__lte=datetime.date(today.year, today.month, monthRange[1]))
    serializer = DayNoteSerializer(queryset, many=True)
    return Response(serializer.data)


@api_view(['post'])
def create(request):
    user = request.user
    day = request.data['day']
    note = request.data['note']

    DayNote.objects.create(
        user=user,
        day=day,
        note=note
    )
    return Response(status=status.HTTP_201_CREATED)


@api_view(['delete'])
def remove(request, pk):
    DayNote.objects.get(pk=pk).delete()
    return Response(status=status.HTTP_202_ACCEPTED)
